const Router = require('koa-router')

// 中间件
const {auth} = require('../middleware/auth.middleware')
const {validator, goodsIdValidator} = require('../middleware/cart.middleware')


// 控制器
const {add, 
    findAll, 
    update, 
    remove, 
    selectAll,
    unselectAll} = require('../controller/cart.controller')

const router = new Router({prefix: '/carts'})

// 添加到购物车接口：登录=>格式验证=>商品id是否存在=>添加到数据库表中
router.post('/', auth, validator({goods_id: 'number'}), goodsIdValidator, add)

// 获取购物车列表接口
router.get('/', auth, findAll) 

// 购物车更新接口
router.patch('/:id', auth, validator({
    number: {type: 'number', required: false},
    selected: {type: 'bool', required: false}
}), update)

// 删除购物车接口
router.delete('/', auth, validator({
    ids: 'array'
}), remove)

// 全选与全不选接口
router.post('/selectAll', auth, selectAll)
router.post('/unselectAll', auth, unselectAll)
module.exports = router